package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Order;
import com.health.service.OrderService;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    private Order order_old=null;
    /**
     * 编辑订单-修改预约设置日期
     */
    @PostMapping("/update")
    public Result update(@RequestBody Order order){
        int count=orderService.update(order,order_old);

        if (count>0){
            return new Result(true,"编辑预约成功");
        }

        return new Result(false,"编辑预约失败");
    }

    /**
     * 编辑订单-根据id查询订单,数据回显
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Order order=orderService.findById(id);
        order_old=order;
        return new Result(true,"查询预约成功",order);
    }
    /**
     * 添加订单
     */
    @PostMapping("/add")
    public Result add(@RequestBody Order order){
        int count=orderService.addOrder(order);

        if (count>0){
            return new Result(true,"添加订单成功");
        }

        return new Result(false,"添加订单失败");
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/deleteById/{id}")
    public  Result deleteById(@PathVariable Integer id){
        int count=orderService.deleteById(id);

        if (count>0){
            return new Result(true,"删除订单成功");
        }

        return new Result(false,"删除订单失败");
    }

    /**
     * 订单列表分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow, @PathVariable Integer pageSize, String query){
        PageResult<Order> pageResult=orderService.findPage(pageNow,pageSize,query);

        return new Result(true,"订单列表分页查询成功",pageResult);
    }
}
